package hai.com.android_test.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import hai.com.android_test.R;

/**
 * Created by 黄海 on 2017/3/29.
 */

public class PullRefreshScrollView extends ScrollView {
    View headView;
    TextView tvHead;
    LinearLayout headContainer;
    LinearLayout containerView;

    public PullRefreshScrollView(Context context) {
        this(context, null);
    }

    public PullRefreshScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PullRefreshScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        headView = LayoutInflater.from(context).inflate(R.layout.refresh_header, null);
        tvHead = (TextView) headView.findViewById(R.id.tv_tip);
        headContainer = (LinearLayout) headView.findViewById(R.id.headContainer);
        containerView = new LinearLayout(context);
        containerView.setOrientation(LinearLayout.VERTICAL);
        containerView.addView(headView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        addView(containerView);

        tvHead.measure(0, 0);
        int headHeight = tvHead.getMeasuredHeight();
        headContainer.setPadding(0, -headHeight, 0, 0);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        containerView.addView(getChildAt(0));
    }
}
